﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace DrCarDrive.DBService
{
    class DoctorCarService
    {
        private DrcardriveDB db;

        public DoctorCarService()
        {
            db = new DrcardriveDB();
        }

        public List<tbl_car> getDoctorCarListByUserNameAndPassword(string username, string password)
        {
            try
            {
                return db.Cars.Where(d => d.USER_NAME.Equals(username) && d.PASSWORD.Equals(password)).ToList();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }
        }


        internal int save(tbl_car car)
        {
            DateTime currentDateTime = DateTime.Now;

            car.INSERT_DATE = currentDateTime;
            car.UPDATE_DATE = currentDateTime;

            if (car != null)
            {
                db.Cars.Add(car);
            }
            return db.SaveChanges();
        }

        public List<tbl_car> viewCarList()
        {
            return db.Cars.ToList();
        }

        internal bool inDatabase(tbl_car car)
        {
            return db.Cars.Any(c => c.REG_NO == car.REG_NO);
        }

        internal int remove(tbl_car car)
        {
            if (car != null)
            {
                tbl_car original = (tbl_car)db.Cars.SingleOrDefault(c=>c.REG_NO.Equals(car.REG_NO));
                db.Cars.Remove(original);
            }
            return db.SaveChanges();
        }

        internal int update(tbl_car car)
        {
            DateTime currentDateTime = DateTime.Now;
            car.UPDATE_DATE = currentDateTime;

            if (car != null)
            {
                var original = db.Cars.SingleOrDefault(c => c.REG_NO.Equals(car.REG_NO));
                db.Entry(original).CurrentValues.SetValues(car);
            }
            return db.SaveChanges();
        }
    }
}
